@echo off
chcp 65001 >nul
echo Starting Android build...
echo.

cd backend

echo Checking Go version and supported platforms...
go version
echo.

echo [1/4] Checking supported GOOS/GOARCH combinations...
go tool dist list | findstr linux | findstr arm
echo.

echo [2/4] Building ARM32 version (most compatible)...
set GOOS=linux
set GOARCH=arm
set GOARM=7
set CGO_ENABLED=0
go build -buildvcs=false -ldflags "-s -w" -tags "osusergo netgo static_build" -o ../homebox-android-arm ./app/api

if %ERRORLEVEL% EQU 0 (
    echo ✓ SUCCESS: ARM32 version built - homebox-android-arm
) else (
    echo ✗ FAILED: ARM32 version build failed
)

echo.
echo [3/4] Attempting ARM64 version...
set GOARCH=arm64
set GOARM=
go build -buildvcs=false -ldflags "-s -w" -tags "osusergo netgo static_build" -o ../homebox-android-arm64 ./app/api

if %ERRORLEVEL% EQU 0 (
    echo ✓ SUCCESS: ARM64 version built - homebox-android-arm64
) else (
    echo ✗ FAILED: ARM64 not supported in this Go version
)

echo.
echo [4/4] Building x86 version for compatibility...
set GOARCH=386
go build -buildvcs=false -ldflags "-s -w" -tags "osusergo netgo static_build" -o ../homebox-android-x86 ./app/api

if %ERRORLEVEL% EQU 0 (
    echo ✓ SUCCESS: x86 version built - homebox-android-x86
) else (
    echo ✗ FAILED: x86 version build failed
)

cd ..
echo.
echo ============================================
echo Build Summary:
echo ============================================
dir homebox-android-* 2>nul
echo.
echo Usage Guide:
echo   1. Transfer any built file to your Android device
echo   2. Install Termux from F-Droid or Google Play
echo   3. Copy file to Termux: cp /sdcard/homebox-android-* ~/
echo   4. Make executable: chmod +x ~/homebox-android-*
echo   5. Run: ./homebox-android-* --config config-android.yml
echo.
echo Architecture Guide:
echo   - homebox-android-arm: Most compatible (32-bit ARM)
echo   - homebox-android-arm64: Modern devices (64-bit ARM)
echo   - homebox-android-x86: Intel-based Android devices
echo.
pause